<?php

namespace mpend\modules\shop\models;

use Yii;
use yii\data\ActiveDataProvider;
use yii\db\Expression;

/**
 * 订单查询模型
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class OrderSearch extends \common\logics\ShopOrderLogic implements \common\db\SearchModelInterface {

    public $type;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['type'], 'safe'],
        ];
    }

    public function getProducts() {
        return static::groupFormat($this->goods, $this->extrasData);
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params 检索参数
     * @return ActiveDataProvider
     */
    public function buildQuery() {
        $query = static::findActive('t')
        ->andWhere([
            't.user_id' => $this->user_id,
        ])
        ->andWhere([
            'or',
            ['>', 't.status', static::STATUS_ACTIVE],
            [
                'and',
                ['status' => [self::STATUS_DEFAULT, self::STATUS_ACTIVE]],
                ['>', 'created_at', time() - self::EXPIRED_DURTION],
            ]
        ]);

        $type = $this->type;
        if ($type == 'payment') {
            //待支付
            $query->andWhere([
                'and',
                ['is', 'out_trade_no', null],
                new Expression('pay_time < created_at')
            ]);
        } elseif ($type == 'deliver') {
            //待发货
            $query->andWhere([
                'and',
                ['status' => self::STATUS_PAID],
                ['is not', 'out_trade_no', null],
                new Expression('pay_time >= created_at')
            ]);
        } elseif ($type == 'receive') {
            //待收货
            $query->andWhere([
                'and',
                ['status' => self::STATUS_SHIP],
                ['is not', 'out_trade_no', null],
                new Expression('pay_time >= created_at')
            ]);
        } elseif ($type == 'evaluate') {
            //已完成
            $query->andWhere([
                'and',
                ['>=', 'status' => self::STATUS_RECEIVED],
                ['is not', 'out_trade_no', null],
                new Expression('pay_time >= created_at')
            ]);
        }
        $query->orderBy([
            'updated_at' => SORT_DESC
        ]);

        Yii::error([
            'request' => Yii::$app->request->getQueryParams(),
            'type' => $type,
            'sql' => $query->debug()
        ]);

        return $query;
    }

    public function fields() {
        return [
            'id',
            'order_no',
            'products',
            'amount',
            'extras',
            'discount',
            'pay' => 'price',
            'express_price',
            'order_time' => function() {
                return date('m-d H:i', $this->created_at);
            },
            'status_text' => 'statusText',
            'handle',
            'service',
        ];
    }

}
